/*
 * Zetor csapat PROTO
 * 
 * 
 */
package continuity;

import java.util.StringTokenizer;

public class ProtoInterpreter {
    Proto proto;
    public ProtoInterpreter(Proto p) {
        proto = p;
    }
    public void interpret(String input) {
        // keszitunk egy string tokenizert, es a bejovo stringet ertelmezzuk
        // a bemeneti formatum altal definialt 
        // parancsokat ertelmezzuk itt
        StringTokenizer tokenizer = new StringTokenizer(input, "   ");
                    String command = tokenizer.nextToken();
                    // kielpes
                    if ("quit".startsWith(command)) {
                          proto.quit();  
                    }
                    // tick kuldese
                    else if ("Tick".startsWith(command)) {
                          int number = getNextArg(tokenizer, 1);
                          proto.tick(number);
                    }
                    //game betoltese, megadott fajlbol stb.
                    else if ("loadGame".startsWith(command)) {
                        String file = getNextCharArg(tokenizer, "");
                        if(file.isEmpty()) {
                            System.out.println("Tul keves parameter!");
                        }
                        proto.loadGame(file); 
                        System.out.println("Game loaded...");
                    }
                    else if ("loadTestFile".startsWith(command)) {
                        String file = getNextCharArg(tokenizer, "");
                        if(file.isEmpty()) {
                            System.out.println("Tul keves parameter!");
                        }
                        proto.loadGame(file); 
                    }
                    //kilistazza az elerheto szinteket
                    else if ("showLevels".startsWith(command)) {
                        proto.showLevels(); 
                    }
                    // kilistazza az elerheto LevelItemeket
                    else if ("showLevelItems".startsWith(command)) {
                        String name = getNextCharArg(tokenizer, "");
                        proto.showLevelItems(name);
                    }
                    //kilistazza az elerheto spriteokat
                    else if ("showSprites".startsWith(command)) {
                        String name = getNextCharArg(tokenizer, "");
                        proto.showSprites(name);
                    }
                    // uj szint letrehozasa
                    else if ("createLevel".startsWith(command)) {
                        String name = getNextCharArg(tokenizer, "");
                        proto.createLevel(name);
                    }
                    // LevelItem hozzaadasa a szinthez
                    else if ("addLevelItem".startsWith(command)) {
                        String to = getNextCharArg(tokenizer, "");
                        String from = getNextCharArg(tokenizer, "");
                        proto.addLevelItem(to, from);
                    }
                    // sprite hozzaadasa a levelitemhez
                    else if ("addSprite".startsWith(command)) {
                        String to = getNextCharArg(tokenizer, "");
                        String from = getNextCharArg(tokenizer, "");
                        proto.addSprite(to, from);
                    }
                    // LevelItem letrehozasa
                    else if ("createLevelItem".startsWith(command)) {
                        int x = getNextArg(tokenizer, 0);
                        int y = getNextArg(tokenizer, 0);
                        String name = getNextCharArg(tokenizer, "");
                        proto.createLevelItem(name, x, y);
                    }
                    // sprite letrehozasa
                    else if ("createSprite".startsWith(command)) {
                        String type = getNextCharArg(tokenizer, "");
                        int x = getNextArg(tokenizer, 0);
                        int y = getNextArg(tokenizer, 0);
                        String name = getNextCharArg(tokenizer, "");
                        int width = getNextArg(tokenizer, -1);
                        int height = getNextArg(tokenizer, -1);
                        // ha kaptunk plussz parametert atadjuk
                        if(width != -1 && height != -1) {
                            Object[] o={width, height};
                            proto.createSprite(type,x,y, name, o);
                        } else {
                            proto.createSprite(type,x,y, name, null);
                        }
                    }
                    // ora frekvenciajanak beallitasa
                    else if ("setPeriod".startsWith(command)) {
                        int number = getNextArg(tokenizer, 1000);
                        proto.setPeriod(number);
                        System.out.println("Period set...");
                    }
                    // aktualis nezet lekerdezese
                    else if ("getView".startsWith(command)) {
                        proto.getView();
                    }
                    // jatekos sebessegenek beallitasa
                    else if ("setPlayerSpeed".startsWith(command)) {
                        int speed = getNextArg(tokenizer, 1);
                        int player = getNextArg(tokenizer, 1);
                        proto.setPlayerSpeed(speed, player);
                    }
                    // mozgas iranyanak meghatarozasa
                    else if ("setMoveDir".startsWith(command)) {
                        int dir = getNextArg(tokenizer, 0);
                        int player = getNextArg(tokenizer, 1);
                        proto.setMoveDir(dir, player);
                    }
                    // player mozgatasa, parametrkent a player szama
                    // alapertelmezesben az elso player mozog
                    else if ("PlayerMove".startsWith(command)) {
                        int player = getNextArg(tokenizer, 1);
                        proto.playerMove(player);
                    }
                    // a palyaelemek atrendezese
                    else if ("moveEmptyItem".startsWith(command)) {
                        int dir = getNextArg(tokenizer, 1);
                        proto.moveEmptyItem(dir);
                    }
                    // nezet valtoztatas
                    else if ("changeView".startsWith(command)) {
                        int number = getNextArg(tokenizer, -1);
                        if(number != -1) proto.changeView(number);
                        else proto.changeView(0);
                    }
    }
    // a tokenizer segitsegevel ertelmezzuk a kovetkezo paramteret
    // integert varunk
    // a parameter hianya, vagy hibas formatum eseten 
    // a visszateresi ertek a def-ben definialt ertek
    private int getNextArg(StringTokenizer tokenizer, int def) {
        int number=def;
        try {
            String arg = tokenizer.nextToken();
            try {
                number = Integer.parseInt(arg);
            } catch (Exception e) {
                number = def;
            }
        } catch (Exception e) {
            number = def;
        }
        return number;
    }
    // a tokenizer segitsegevel ertelmezzuk a kovetkezo paramteret
    // stringet varunk
    // a parameter hianya, vagy hibas formatum eseten 
    // a visszateresi ertek a def-ben definialt ertek
    private String getNextCharArg(StringTokenizer tokenizer, String def) {
        String arg = def;
        try {
            arg = tokenizer.nextToken();
        } catch (Exception e) {
            arg= def;
        }
        return arg;
    }
    
    
}
